﻿using System;
using System.Web.Mvc;
using CMSMVC4.Areas.Admin.Models;
using CMSMVC4.Filters;
using System.Linq;

namespace CMSMVC4.Areas.Admin.Controllers
{
    [Authorize(Roles = "Admin,Contact")]
    public class ContactController : Controller
    {
        private static readonly CMSMVC4Entities Db = new CMSMVC4Entities();
        //
        // GET: /Admin/Contact/ListContact
        #region
        [HttpGet]
        public ActionResult ListContact()
        {
            var table = new CommandSql().GetAllContacts();

            ViewBag.M = new CommandSql().GetParrentByMenu("/Admin/Contact/ListContact").Rows[0]["Parrent"];
            return View(table);
        }

        #endregion

        //
        // GET: /Admin/Contact/AddContact
        #region
        [HttpGet]
        public ActionResult AddContact()
        {
            ViewBag.lang = new CommandSql().GetLanguage();
            ViewBag.M = new CommandSql().GetParrentByMenu("/Admin/Contact/AddContact").Rows[0]["Parrent"];
            return View();
        }

        [HttpPost]
        public ActionResult AddContact(ContactModels models)
        {
            if (ModelState.IsValid)
            {
                var contact = new Contact();
                var contactLang1 = new ContactLang();
                var contactLang2 = new ContactLang();


                // add to Contact
                contact.Phone = models.Phone;
                contact.Fax = models.Fax;
                contact.Email = models.Email;
                contact.Url = models.Map;
                contact.Website = models.Website;
                Db.Contacts.AddObject(contact);
                Db.SaveChanges();

                // add to ContactLang
                var table = new CommandSql().GetContactDesc();
                for (var i = 0; i < table.Rows.Count; i++)
                {
                    contactLang1.LanguageCode = "en-us";
                    contactLang1.ContactBranch = Convert.ToInt32(table.Rows[i]["Branch"].ToString());
                    contactLang1.Name = models.CompanyVi;
                    contactLang1.Address = models.AddressEn;
                    contactLang1.Description = models.DisEn;
                    Db.ContactLangs.AddObject(contactLang1);

                    contactLang2.LanguageCode = "vi-vn";
                    contactLang2.ContactBranch = Convert.ToInt32(table.Rows[i]["Branch"].ToString());
                    contactLang2.Name = models.CompanyVi;
                    contactLang2.Address = models.AddressVi;
                    contactLang2.Description = models.DisVi;
                    Db.ContactLangs.AddObject(contactLang2);
                }

                Db.SaveChanges();
                return RedirectToAction("ListContact");
            }
            return View("AddContact", models);
        }
        #endregion


        // 
        // GET: /Admin/Contact/EditContactJson
        #region
        [HttpGet]
        public ActionResult EditContact(int id)
        {
            var data = new CommandSql().GetOneContact(id);
            ViewBag.AddVi =
                new CommandSql().GetOneContactByLang(id, "vi-vn").Rows[0]["Address"].ToString();
            ViewBag.addEn =
                new CommandSql().GetOneContactByLang(id, "en-us").Rows[0]["Address"].ToString();
            //ViewBag.DisEn = new CommandSql().GetOneContactByLang(id, "en-us").Rows[0]["Description"].ToString();
            //ViewBag.DisVi = new CommandSql().GetOneContactByLang(id, "vi-vn").Rows[0]["Description"].ToString();
            ViewBag.Data = data.Rows[0];
            return PartialView();
        }

        [HttpPost]
        public ActionResult EditContact(int id, ContactModels models)
        {
            if (ModelState.IsValid)
            {
                var contact = Db.Contacts.SingleOrDefault(x => x.Branch == id);
                if (contact != null)
                {
                    contact.Phone = models.Phone;
                    contact.Fax = models.Fax;
                    contact.Email = models.Email;
                    contact.Website = models.Website;
                    contact.Url = models.Map;
                }
                var culture = new CommandSql().GetLanguage();
                var lang = Db.ContactLangs.SingleOrDefault(x => x.ContactBranch == id && x.LanguageCode == culture);
                if (lang != null)lang.Name = models.CompanyVi;
                
                var langVi = Db.ContactLangs.SingleOrDefault(x => x.ContactBranch == id && x.LanguageCode == "vi-vn");
                if (langVi != null) langVi.Address = models.AddressVi;
                if (langVi != null) langVi.Description = models.DisVi;

                var langEn = Db.ContactLangs.SingleOrDefault(x => x.ContactBranch == id && x.LanguageCode == "en-us");
                if (langEn != null) langEn.Address = models.AddressEn;
                if (langEn != null) langEn.Description = models.DisEn;

                Db.SaveChanges();

                if (Request.IsAjaxRequest())
                {
                    return JavaScript("window.location='/Admin/Contact/ListContact'");
                }
                return RedirectToAction("EditContact");
            }
            if (Request.IsAjaxRequest())
            {
                return PartialView("EditContact", models);
            }
            return View("EditContact", models);
        }
        #endregion

        //
        // GET: /Admin/Contact/DeleteContactJson
        #region
        [HttpGet]
        public ActionResult DeleteContact(int id)
        {
            ViewBag.Data = new CommandSql().GetOneContact(id).Rows[0];
            return PartialView();
        }

        [HttpPost]
        public ActionResult DeleteContact(string[] id)
        {
            if (id != null)
            {
                foreach (var i in id)
                {
                    int idcomment;
                    int.TryParse(i, out idcomment);
                    var row = Db.Contacts.FirstOrDefault(x => x.Branch == idcomment);
                    Db.Contacts.DeleteObject(row);
                    Db.SaveChanges();
                }
            }
            return Json(string.Empty, JsonRequestBehavior.AllowGet);
        }
        #endregion

        public ActionResult Index()
        {
            Category cate = Db.Categories.FirstOrDefault(x => x.Type == MenuType.LienHe);
            if (cate == null)
            {
                Category ctn = new Category();
                ctn.Type = MenuType.LienHe;
                ctn.Status = CategoriStatus.Thuong;
                ctn.Sort = 1;
                Db.Categories.AddObject(ctn);
                Db.SaveChanges();
                cate = Db.Categories.FirstOrDefault(x => x.Type == MenuType.LienHe);
            }
            Page page = Db.Pages.FirstOrDefault(x => x.CategoriesID == cate.Id);
            if (page == null)
            {
                Page pn = new Page();
                pn.CategoriesID = cate.Id;
                pn.Status = PageStatus.GioiThieu;
                pn.Tab = 1;
                pn.View = 0;
                Db.Pages.AddObject(pn);
                Db.SaveChanges();
                page = Db.Pages.FirstOrDefault(x => x.CategoriesID == cate.Id);
            }
            PageLang pagelangv = Db.PageLangs.FirstOrDefault(x => x.PageId == page.Id && x.LanguageCode.Equals("vi-vn"));
            PageLang pagelange = Db.PageLangs.FirstOrDefault(x => x.PageId == page.Id && x.LanguageCode.Equals("en-us"));
            if (pagelangv == null)
            {
                PageLang pgn = new PageLang();
                pgn.Name = "Liên Hệ";
                pgn.LanguageCode = "vi-vn";
                pgn.PageId = page.Id;
                Db.PageLangs.AddObject(pgn);
                Db.SaveChanges();
                pagelangv = Db.PageLangs.FirstOrDefault(x => x.PageId == page.Id && x.LanguageCode.Equals("vi-vn"));
            }
            if (pagelange == null)
            {
                PageLang pgn = new PageLang();
                pgn.Name = "Conntact";
                pgn.LanguageCode = "en-us";
                pgn.PageId = page.Id;
                Db.PageLangs.AddObject(pgn);
                Db.SaveChanges();
                pagelange = Db.PageLangs.FirstOrDefault(x => x.PageId == page.Id && x.LanguageCode.Equals("en-us"));
            }
            GioiThieuModel model = new GioiThieuModel();
            model.id = page.Id;

            model.Pageidv = pagelangv.Id;
            model.fullv = pagelangv.FullContent;
            model.sumv = pagelangv.Summary;

            model.Pageide = pagelange.Id;
            model.fulle = pagelange.FullContent;
            model.sume = pagelange.Summary;
            return View(model);
        }

        [HttpPost]
        public ActionResult Index(GioiThieuModel model)
        {
            if (ModelState.IsValid)
            {
                PageLang pagelangv = Db.PageLangs.FirstOrDefault(x => x.Id == model.Pageidv && x.LanguageCode.Equals("vi-vn"));
                PageLang pagelange = Db.PageLangs.FirstOrDefault(x => x.Id == model.Pageide && x.LanguageCode.Equals("en-us"));
                if (pagelangv != null)
                {
                    pagelangv.FullContent = model.fullv;
                    pagelangv.Summary = model.sumv;
                    Db.SaveChanges();
                }
                if (pagelange != null)
                {
                    pagelange.FullContent = model.fulle;
                    pagelange.Summary = model.sume;
                    Db.SaveChanges();
                }
                ModelState.AddModelError("", "Successfull");
            }
            return View(model);
        }
    }
}
